<?php

class UserController extends Zend_Controller_Action
{
    public function init()
    {
    	
    }

    //http://localhost/trombino/public/index.php/user/list
    public function listAction()
    {  	
    	$db = new Model_DbTable_Flux_Uti();    	 
    	$arr = $db->getAll();    	 
    	$this->view->data = $arr;
    }
    
    public function registerAction()
    {
    	if ($this->getRequest ()->isPost ()) 
    	{
    		$login = $this->getRequest()->getPost('txtLogin');    		
    		$maj = $this->getRequest()->getPost('txtMaj');
    		$flux = $this->getRequest()->getPost('txtFlux');
    		$mdp = $this->getRequest()->getPost('txtMdp');
    		$role = $this->getRequest()->getPost('txtRole');
    		$mdpSel = $this->getRequest()->getPost('txtMdpSel');
    		$mail = $this->getRequest()->getPost('txtMail');
    		
    		$ip = $this->get_client_ip();
    		$date = new Zend_Db_Expr('NOW()');
    		
    		$db = new Model_DbTable_Flux_Uti();
    		$uid = $db->ajouter(array("login"=>$login, "maj"=>$maj, "flux"=>$flux,
    					"mdp"=>$mdp, "role"=>$role, "mdp_sel"=>$mdpSel,
    					"email"=>$mail, "ip_inscription"=>$ip, "date_inscription"=>$date
    				)); 	
    		
    		//$this->_redirect ('/user/detail/id/'. $login);
    		$this->_redirect ('/user/list');
    	}    
    	else 
    	{    	
    		$this->view->title = "Welcome";    	
    		$this->render ();    	
    	}    	 
    }
    
        
    public function detailAction()
    {
    	$id =  $this->getRequest()->getParam('id');
    	
    	$db = new Model_DbTable_Flux_Uti();
    	$this->view->data = $db->findByuti_id($id);
    }   
    
    public function supprimerAction()
    {
    	$id = $this->getRequest()->getParam('id');
    	$db = new Model_DbTable_Flux_Uti();
    	$db->remove($id);
    	$this->_redirect ('/user/list');
    }
    
    public function modifyAction()
    {
    	if ($this->getRequest ()->isPost ())
    	{
	    	$id = $this->getRequest()->getPost('txtId');
	    	$login = $this->getRequest()->getPost('txtLogin');
	    	$maj = $this->getRequest()->getPost('txtMaj');
	    	$flux = $this->getRequest()->getPost('txtFlux');
	    	$mdp = $this->getRequest()->getPost('txtMdp');
	    	$role = $this->getRequest()->getPost('txtRole');
	    	$mdpSel = $this->getRequest()->getPost('txtMdpSel');
	    	$mail = $this->getRequest()->getPost('txtMail');
	
	    	$db = new Model_DbTable_Flux_Uti();
	    	$db->edit($id, array("login"=>$login, "maj"=>$maj, "flux"=>$flux,
	    			"mdp"=>$mdp, "role"=>$role, "mdp_sel"=>$mdpSel,
	    			"email"=>$mail
	    	));
	
	    	$this->_redirect ('/user/list');
    	}
    }

    function get_client_ip()
    {
    	global  $_SERVER;
    	if  (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
    	{
    		$realip  =  $_SERVER
    
    		["HTTP_X_FORWARDED_FOR"];
    	}
    	elseif  (isset($_SERVER["HTTP_CLIENT_IP"]))
    	{
    		$realip  =  $_SERVER["HTTP_CLIENT_IP"];
    	}
    	else
    	{
    		$realip  =  $_SERVER["REMOTE_ADDR"];
    	}
    	return  $realip;
    }  
    
}